package com.example.stock.service;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONUtil;
import com.example.stock.entity.Stock;
import com.example.stock.util.HolidayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class YangDingTianService {

    @Autowired
    private IStockService stockService;

//    @PostConstruct
    public void init() throws ParseException {
        LocalDate startDay = LocalDate.parse("2024-12-26", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate endDay = LocalDate.parse("2025-03-10", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        for (int j = 0; j < 30000; j++) {
            if (startDay.isAfter(endDay)) {
                break;
            }
            if(!isJiaoyiDay(startDay.atStartOfDay())){
                startDay = startDay.plusDays(1);
                continue;
            }
            String day3 = startDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            //昨天
            String day2 = getLastGzr(LocalDate.parse(day3, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
            //前天
            String day1 = getLastGzr(LocalDate.parse(day2, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
            //后天
            String day4 = getNextGzr(LocalDate.parse(day3, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());

            List<Stock> stocks = stockService.calcYangDingTian(day1, day2, day3);
            List<Stock> stocks1 = stockService.listByCalcTimeAndCalcType(day1, 3);
            List<Stock> stocks2 = stockService.listByCalcTimeAndCalcType(day3, 3);
            List<Stock> stocks3 = stockService.listByCalcTimeAndCalcType(day2, 3);
            List<Stock> stocks4 = stockService.listByCalcTimeAndCalcType(day4, 3);
            Map<String, Stock> map = stocks2.stream().collect(Collectors.toMap(Stock::getCode, Function.identity()));
            Map<String, Stock> map1 = stocks1.stream().collect(Collectors.toMap(Stock::getCode, Function.identity()));
            Map<String, Stock> map3 = stocks3.stream().collect(Collectors.toMap(Stock::getCode, Function.identity()));
            Map<String, Stock> map4 = stocks4.stream().collect(Collectors.toMap(Stock::getCode, Function.identity()));
            Console.log(day3 + "日数据：");
            for (int i = 0; i < stocks.size(); i++) {
                Stock stock = stocks.get(i); //早盘
                Stock stock1 = map.get(stock.getCode()); //尾盘
                Stock stock3 = map3.get(stock.getCode()); // 昨收盘
                Stock stock2 = map1.get(stock.getCode()); // 前收盘
                boolean state1 = stock1.getChangePercent() < 0 && stock1.getChangePercent() > -1; //开盘小于1
//                double zhengfu = stock1.getPriceMax() - stock1.getPriceMin();
//                double yinti = stock.getPriceNow() - stock1.getPriceNow();
//                boolean state2 = (yinti > 0 && zhengfu - yinti > yinti);
//                boolean state3 = stock3.getAmplitude() < 8;
//                boolean state4 = stock3.getPriceMin() > stock1.getPriceMin();
//                boolean state5 = stock3.getPriceMin() <= stock2.getPriceNow();
                boolean state6 = stock.getChangePercent() > 1; //开盘大于1
              //  boolean state5 = stock1.getVolumeCompare() > 1.8*stock3.getVolumeCompare();
//                boolean state7 = stock2.getVolumeCompare() > 1.8*stock3.getVolumeCompare();
                List<Stock> stocks5 = stockService.maxStock(stock.getCode(), startDay.plusDays(-14).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), day2);

                if (state1 && state6 && Objects.equals(stocks5.get(0).getId(), stock3.getId())) {
                    Stock stock4 = map4.get(stock.getCode());
                    Console.log(stock.getName() + "---" + stock.getCode() + "----(" + stock4.getChangePercent()+")");
                }
            }
            startDay = startDay.plusDays(1);
        }
    }


//    @PostConstruct
    public void init2() throws ParseException {
        LocalDate startDay = LocalDate.parse("2024-12-26", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate endDay = LocalDate.parse("2025-03-10", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        for (int j = 0; j < 30000; j++) {
            if (startDay.isAfter(endDay)) {
                break;
            }
            if(!isJiaoyiDay(startDay.atStartOfDay())){
                startDay = startDay.plusDays(1);
                continue;
            }
            String day3 = startDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            //昨天
            String day2 = getLastGzr(LocalDate.parse(day3, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
            //前天
            String day1 = getLastGzr(LocalDate.parse(day2, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
            //明天
            String day4 = getNextGzr(LocalDate.parse(day3, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());

            List<Stock> stocks = stockService.calcStock(day2, day3, day4);

            Console.log(day3 + "日数据：");
            for (int i = 0; i < stocks.size(); i++) {
                Stock stock = stocks.get(i);
                Console.log(stock.getName() + "---" + stock.getCode() + "----(" + stock.getChangePercent()+")");
            }
            startDay = startDay.plusDays(1);
        }
    }
    private boolean isJiaoyiDay(LocalDateTime nowLocal) throws ParseException {
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化当前时间
        String formattedNow = nowLocal.format(formatter);
        String workingDay = HolidayUtil.isWorkingDay(formattedNow);
        if(!"0".equals(workingDay)){
           // Console.log(formattedNow+"不是工作日不处理refresh3");
            return false;
        }
        return true;
    }



    public String getLastGzr(LocalDateTime nowLocal) throws ParseException {
        String calcDate = null;
        for (int i = 1; i < 10; i++) {
            nowLocal = nowLocal.plusDays(-1);
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 格式化当前时间
            String formattedNow = nowLocal.format(formatter);
            String workingDay = HolidayUtil.isWorkingDay(formattedNow);
            if("0".equals(workingDay)){
                calcDate =  formattedNow;
                break;
            }
        }
        return calcDate;
    }

    public String getNextGzr(LocalDateTime nowLocal) throws ParseException {
        String calcDate = null;
        for (int i = 1; i < 10; i++) {
            nowLocal = nowLocal.plusDays(1);
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 格式化当前时间
            String formattedNow = nowLocal.format(formatter);
            String workingDay = HolidayUtil.isWorkingDay(formattedNow);
            if("0".equals(workingDay)){
                calcDate =  formattedNow;
                break;
            }
        }
        return calcDate;
    }
}
